﻿using BaoVeThanhDia.GamePlay.Monsters;
using BaoVeThanhDia.MapFolder;
using BaoVeThanhDia.Sound;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace BaoVeThanhDia.GamePlay
{
    public class Weapon
    {
        private int Type;// 1 là cung, 2 là bom, 3 là tên lửa, 4 là đạn xanh, 5 là đạn xanh lá
        private float PhysicDame;
        private float MagicDame;
        private float Speed;
        private Vector2 startPo, curPo, stopPo, duongdi;
        private int target;
        private float rotation;
        private Texture2D Image;
        private bool isEnable;
        private string Effect;
        private float countEffect;
        private int TimeEffect;
        private Vector2 origin;
        private int countRocket = 8;
        CustomSoundEffect satthuong;
        public Weapon()
        {
        }
        public Weapon(int type, float phy, float magic, float speed, Vector2 start, Vector2 tar, int target, string Effect, int TimeEffect, float countEffect = 0)
        {
            satthuong = new CustomSoundEffect("sounds/effect/Monster/Trungdon");
            Type = type;
            PhysicDame = phy;
            MagicDame = magic;
            Speed = speed;
            startPo = start;
            stopPo = tar;
            isEnable = true;
            this.target = target;
            this.Effect = Effect;
            this.countEffect = countEffect;
            this.TimeEffect = TimeEffect;
            getRotation();
        }
        public void getRotation()
        {
            rotation = 0;
            isEnable = true;
            // tính đường đi
            #region
            if (Math.Abs(stopPo.X - startPo.X) > Math.Abs(stopPo.Y - startPo.Y))
            {
                duongdi = new Vector2(1, (stopPo.Y - startPo.Y) / (stopPo.X - startPo.X));
                if (stopPo.X < startPo.X) duongdi *= -1;
            }
            else
            {
                duongdi = new Vector2((stopPo.X - startPo.X) / (stopPo.Y - startPo.Y), 1);
                if (stopPo.Y < startPo.Y) duongdi *= -1;
            }
            #endregion


            //tính góc quay và độ lệch phù hợp 
            #region
            if (duongdi.X == 0)
            {
                if (duongdi.Y > 0)
                {
                    rotation = MathHelper.Pi;
                }
                else
                {
                    rotation = 0;
                }
            }
            else
            {
                if (duongdi.X > 0)
                {
                    rotation = (float)Math.Atan(duongdi.Y / duongdi.X) + MathHelper.Pi / 2;
                }
                else // hướng trái
                {
                    rotation = (float)Math.Atan(duongdi.Y / duongdi.X) - MathHelper.Pi / 2;
                }
            }
            #endregion

            curPo = startPo;

        }
        public void Load(ContentManager content)
        {
            if (Type <= 3)
            {
                Image = content.Load<Texture2D>("images/towers/weapon/" + Type.ToString());
            }
            else
            {
                Image = content.Load<Texture2D>("images/towers/magic/ball" + (Type - 3).ToString());
            }
            satthuong.Load(content);
        }
        public void Draw(SpriteBatch sprite)
        {
            if (isEnable)
            {
                if (Type == 1)
                {
                    origin = new Vector2(9, 48);
                    sprite.Draw(Image, (curPo + origin) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 17, 96), Color.White, rotation, origin, 0.8f * GamePage.Scale, SpriteEffects.None, 0f);
                }
                else if (Type == 2)
                {
                    origin = new Vector2(14, 14);
                    sprite.Draw(Image, (curPo + origin) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 28, 28), Color.White, rotation, origin, 1.5f * GamePage.Scale, SpriteEffects.None, 0f);
                }
                else if (Type == 3)
                {
                    origin = new Vector2(23, 44);
                    sprite.Draw(Image, (curPo + origin) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 45, 87), Color.White, rotation, origin, GamePage.Scale, SpriteEffects.None, 0f);
                }
                else
                {
                    origin = new Vector2(35, 35);
                    sprite.Draw(Image, (curPo + origin) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 69, 69), Color.White, -rotation, origin, 1.4f * GamePage.Scale, SpriteEffects.None, 0f);
                }
            }
        }
        public Texture2D getImage()
        {
            return Image;
        }
        public void DiChuyen()
        {
            bool isNo = false;
            #region thực hiện việc di chuyển
            if (Type == 2)
            {
                if (countRocket > 0)
                {
                    curPo += new Vector2((stopPo.X - curPo.X) / (15 * Speed), -1) * Speed;
                    countRocket--;
                }
                else if (countRocket == 0)
                {
                    startPo = curPo;
                    getRotation();
                    countRocket--;
                }
                else
                {
                    curPo += Speed * duongdi;
                }
            }
            else
                curPo += Speed * duongdi;
            #endregion
            if (Type != 2 && target != -1 && (target <= GamePage.monsters.Count - 1) && GamePage.monsters[target].getIsEnable())
            {
                #region nếu mục tiêu đã chọn đang sống thì tiêu diệt nó
                if (isCollision(GamePage.monsters[target]) || Vector2.Distance(stopPo, curPo) <= (Speed / Math.Sqrt(2)))
                {
                    satthuong.Play(0.6f);
                    GamePage.monsters[target].setTruMau(PhysicDame, MagicDame, Effect, countEffect, TimeEffect);
                    isEnable = false;
                }
                #endregion
            }
            else
            {
                if (!(Type == 2 && countRocket >= 0))
                {
                    #region nếu đạn va chạm với 1 quái vật thì trừ máu
                    for (int i = 0; i < GamePage.monsters.Count; i++)
                    {
                        if (isCollision(GamePage.monsters[i]))
                        {
                            if (Type < 5)
                            {
                                isEnable = false;
                                satthuong.Play(0.6f);
                            }
                            GamePage.monsters[i].setTruMau(PhysicDame, MagicDame, Effect, countEffect, TimeEffect);
                            isNo = true;
                            break;
                        }
                    }
                    #endregion
                    #region nếu tới đích
                    if (Vector2.Distance(stopPo, curPo) <= (Speed / Math.Sqrt(2)) || curPo.X < 0 || curPo.Y < 0 || curPo.X > Map.SizeOfMatrix.Y * 128 || curPo.Y > Map.SizeOfMatrix.X * 128)
                    {
                        isNo = true;
                        isEnable = false;
                    }
                    #endregion
                    #region nếu là mìn thì nổ nhiều quái
                    if (isNo && Type == 2)
                    {
                        for (int i = GamePage.monsters.Count - 1; i > 0; i--)
                        {
                            if (Vector2.Distance(curPo, GamePage.monsters[i].getPosition()) < 256 * GamePage.Scale)
                                GamePage.monsters[i].setTruMau(PhysicDame, MagicDame, Effect, countEffect, TimeEffect);
                        }
                    }
                    #endregion
                }
            }
        }
        public bool getEnable()
        {
            return isEnable;
        }
        public bool isCollision(Monster monster)
        {
            if (curPo.X >= monster.getPosition().X + 10 && curPo.Y >= monster.getPosition().Y + 10)
            {
                if (curPo.X <= monster.getPosition().X + 100 && curPo.Y <= monster.getPosition().Y + 100)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
